package com.google.ma.rssreader;

import java.util.ArrayList;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

public class RssHandler implements ContentHandler {

    private ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
    private HashMap<String, String> map = null;
    private boolean inItem = false;
    private boolean inTitle = false;
    private boolean inLink = false;
    private boolean inDescription = false;
    private boolean inTime = false;

    @Override
    public void characters(char[] ch, int begin, int length) throws SAXException {
        if (inTitle) {
            map.put("title", new String(ch, begin, length));
        }
        if (inLink) {
            map.put("link", new String(ch, begin, length));
        }
        if (inDescription) {
            String des = new String(ch, begin, length);
            des = des.replaceAll("<br />", "\n").replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("<[^>]*>", "").replaceAll("[(/>)<]", "").replaceAll("\n\n", "\n");
            map.put("description", des);
        }
        if (inTime) {
            map.put("time", new String(ch, begin, length));
        }
    }

    @Override
    public void endDocument() throws SAXException {

    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if ("item".equals(localName)) {
            list.add(map);
            inItem = false;
        }
        if (inItem && "title".equals(localName)) {
            inTitle = false;
        }
        if (inItem && "link".equals(localName)) {
            inLink = false;
        }
        if (inItem && "description".equals(localName)) {
            inDescription = false;
        }
        if (inItem && "pubDate".equals(localName)) {
            inTime = false;
        }
    }

    @Override
    public void endPrefixMapping(String arg0) throws SAXException {

    }

    @Override
    public void ignorableWhitespace(char[] arg0, int arg1, int arg2) throws SAXException {

    }

    @Override
    public void processingInstruction(String arg0, String arg1) throws SAXException {

    }

    @Override
    public void setDocumentLocator(Locator arg0) {

    }

    @Override
    public void skippedEntity(String arg0) throws SAXException {

    }

    @Override
    public void startDocument() throws SAXException {

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        if ("item".equals(localName)) {
            map = new HashMap<String, String>();
            inItem = true;
        }
        if (inItem && "title".equals(localName)) {
            inTitle = true;
        }
        if (inItem && "link".equals(localName)) {
            inLink = true;
        }
        if (inItem && "description".equals(localName)) {
            inDescription = true;
        }
        if (inItem && "pubDate".equals(localName)) {
            inTime = true;
        }
    }

    @Override
    public void startPrefixMapping(String arg0, String arg1) throws SAXException {

    }

    public ArrayList<HashMap<String, String>> getList() {
        return list;
    }
}